package cn.zhl.base;

import cn.zhl.base.enums.ResultCode;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @program: portal
 * @description: 返回值包装类
 * @author: 刘振华
 * @create: 2019-06-20 17:07
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class BaseResult implements Serializable {
	private static final long serialVersionUID = -3948389268046368059L;
	private Integer code;
	private String msg;
	private Object data;

	public BaseResult(Integer code, String msg) {
		this.code = code;
		this.msg = msg;
	}

	public static BaseResult success() {
		return success(null);
	}

	public static BaseResult success(Object data) {
		return BaseResult.builder()
			.data(data)
			.build().setResultCode(ResultCode.SUCCESS);
	}

	public static BaseResult fail(ResultCode rs, Object data) {
		return BaseResult.builder().data(data).build().setResultCode(rs);
	}

	public static BaseResult fail(ResultCode rs) {
		return fail(rs, null);
	}

	private BaseResult setResultCode(ResultCode rs) {
		this.setCode(rs.code());
		this.setMsg(rs.message());
		return this;
	}
}
